/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ericsson.nfc.signing.algorithm;

import com.ericsson.nfc.signing.record.SignatureField;
import com.ericsson.nfc.signing.record.SignatureRecord;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;

//#ifdef MyJSR257
//# import my.javax.microedition.contactless.ndef.NDEFRecordType;
//#else
import javax.microedition.contactless.ndef.NDEFRecordType;
//#endif

/**
 *
 * @author emarkki
 */
public class DefaultVerifierFactory extends VerifierFactory {

//    private Hashtable verifiers = new Hashtable(5);
    
    public DefaultVerifierFactory() {
    }

//    
//    public static SignatureFactory getInstance() {
//        if(instance == null) {
//            instance = new SignatureFactory();
//        }
//        return instance;
//    }
//    
    
    public SignatureVerifier getVerifier(NDEFRecordType signatureRecordType, int signatureType) {
        
        SignatureVerifier verifier = null;

        switch(signatureType) {
            case SignatureField.RSASSA_PSS: {
                verifier = RSASSAPSSVerifier.getInstance();
                break;
            }
            case SignatureField.RSASSA_PKCS1_V1_5_WITH_SHA1: {
                if(ImplementationDetector.getInstance().isSatsaAvailable()) {
                    verifier = RsaSsaPkcs1V15SatsaVerifier.getInstance();
                }

                if(verifier == null) {
                    verifier = RsaSsaPkcs1v15BcVerifier.getInstance();
                }
                break;
            }
            case SignatureField.DSA: {
                if(ImplementationDetector.getInstance().isSatsaAvailable()) {
                    verifier = DSASatsaVerifier.getInstance();
                }

                if(verifier == null) {
                    verifier = DSAVerifier.getInstance();
                }
                break;
            }
            case SignatureField.ECDSA_P_192_SHA1: {
                verifier = ECDSAVerifier.getInstance();
                break;
            }
        }
        
        return verifier;
    }

    public SignatureVerifier getVerifier(DERObjectIdentifier algorithmObjectId) {
        if (PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.RSASSA_PKCS1_V1_5_WITH_SHA1);
        } else if (ECDSAVerifier.SHA1withECDSA.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.ECDSA_P_192_SHA1);
        } else if(DSAVerifier.id_dsa_with_sha1.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.DSA);
        } else if(PKCSObjectIdentifiers.id_RSASSA_PSS.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.RSASSA_PSS);
        }
        return null;
    }
    
}
